package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.ResponseBean;
import com.pojo.Role;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private RoleService iRoleService;

    /**
     *查询角色
     * @return
     */
    @RequestMapping("/selRole")
    public ResponseBean selRole(){
        List list = iRoleService.selRole();
        if(list!=null&&list.size()>0){
            return new ResponseBean(200 , "角色查询成功" , list);
        }else{
            return new ResponseBean(500 , "角色查询失败" , null);
        }
    }

    @RequestMapping("/findRoleWithPage")
    public ResponseBean findRoleWithPage(int page, int limit, String roleName){
        IPage<Role> page1 = new Page<Role>(page,limit);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
        if(roleName!=null&&roleName.trim().length()>0) {
            queryWrapper.like("role_name", roleName);
        }
        IPage iPage = iRoleService.page(page1,queryWrapper);
        return new ResponseBean(0,iPage.getTotal(),iPage.getRecords());
    }

    /**
     * 修改角色名称
     */
    @RequestMapping("/updateRoleName")
    public ResponseBean updateRole(String roleId, String roleName){

//        System.out.println("角色ID："+roleId+" 角色名称："+roleName);
        int i = iRoleService.countName(roleName);
        int m = iRoleService.countNameID(roleId,roleName);
//        System.out.println("i="+i+" m="+m);
        if(i>=1 && m==0){
            return new ResponseBean(2, 1, "该角色名称已存在");
        }else{
            Role role = new Role();
            role.setRoleId(roleId);
            role.setRoleName(roleName);
            if(iRoleService.updateById(role)){
                return new ResponseBean(0, 1, "修改成功");
            }else{
                return new ResponseBean(1, 1, "修改失败");
            }
        }
    }
}
